<?php

namespace mpend\modules\user\controllers;

use Yii;
use mpend\components\Controller;
use common\logics\UserLogic;
use common\helpers\Url;

/**
 * 个人主页相关接口
 * Default controller for the `user` module
 */
class HomeController extends Controller {

    /**
     * Declares the allowed HTTP verbs.
     * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
     * @return array the allowed HTTP verbs.
     */
    protected function verbs() {
        return [
            '*' => ['POST'],
            'index' => ['GET'],
        ];
    }

    /**
     * Declares the allowed HTTP verbs.
     * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
     * @return array the allowed HTTP verbs.
     */
    protected function rules() {
        return [
            [
                'allow' => true,
                'roles' => ['@'],
            ],
        ];
    }

    public $modelClass = 'mpend\modules\user\models\HomeSearch';

    /**
     * {@inheritdoc}
     */
    public function actions() {
        return [
            'view' => [
                'class' => 'yii\rest\ViewAction',
                'modelClass' => $this->modelClass,
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actionIndex() {
        $user = UserLogic::findOne($this->token->user_id);
        $menus = [];
        if ($user->vip) {
            $menus[] = [
                [
                    'icon' => Url::attach('assets/icons/credit.png'),
                    'label' => '积分中心',
                    'link' => Url::h5('credit'),
                ],
                [
                    'icon' => Url::attach('assets/icons/coupon.png'),
                    'label' => '活动卡券',
                    'link' => Url::h5('coupon'),
                ],
            ];
//            $menus[] = [
//                [
//                    'icon' => Url::attach('assets/icons/member.png'),
//                    'label' => '会员服务',
//                    'link' => Url::h5('member'),
//                ],
//            ];
//            $menus[] = [
//                [
//                    'icon' => Url::attach('assets/icons/commission.png'),
//                    'label' => '分销中心',
//                    'link' => Url::h5('commission'),
//                ],
//                [
//                    'icon' => Url::attach('assets/icons/train.png'),
//                    'label' => '培训学院',
//                    'link' => Url::h5('college'),
//                ],
//                [
//                    'icon' => Url::attach('assets/icons/statement.png'),
//                    'label' => '政策公告',
//                    'link' => Url::h5('statement'),
//                ],
//            ];
        }
        $advert = [
            'thumb' => Url::attach('assets/advert/uc_750_200.jpg'),
            'link' => Url::h5('adverts'),
        ];
        $perfect = [];
        if ($user->perfect < 85) {
            $perfect = [
                'icon' => Url::attach('assets/icons/certificate.png'),
                'text' => '资料完善度',
                'percent' => $user->perfect,
                'tips' => '完善获得更多权益',
            ];
        }
        $ordertabs = [
            [
                'index' => 1,
                'name' => '全部订单',
                'link' => Url::h5('order/list/all'),
                'badge' => '',
            ],
            [
                'index' => 2,
                'name' => '待付款',
                'link' => Url::h5('order/list/payment'),
                'badge' => '1',
            ],
            [
                'index' => 3,
                'name' => '待收货',
                'link' => Url::h5('order/list/receive'),
                'badge' => '',
            ],
            [
                'index' => 4,
                'name' => '待评价',
                'link' => Url::h5('order/list/evaluate'),
                'badge' => '',
            ],
        ];
        $service_url = Url::h5('order/service');
        return compact('user', 'perfect', 'ordertabs', 'service_url', 'advert', 'menus');
    }

}
